#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct
{
    int* arr;
    int capacity;
    int front;
    int rear;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* as = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    as->arr = (int*)malloc(sizeof(int) * (k + 1));
    as->capacity = k;
    as->front = as->rear = 0;
    return as;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->front == obj->rear;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return (obj->rear + 1) % (obj->capacity + 1) == obj->front;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (myCircularQueueIsFull(obj))
    {
        return false;
    }
    obj->arr[obj->rear] = value;
    obj->rear = (obj->rear + 1) % (obj->capacity + 1);
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return false;
    }
    obj->front = (obj->front + 1) % (obj->capacity + 1);
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    return obj->arr[obj->front];
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    if (obj->rear == 0)
    {
        return obj->arr[obj->capacity];
    }
    else
    {
        return obj->arr[obj->rear - 1];
    }
}



void myCircularQueueFree(MyCircularQueue* obj) {

    if (obj->arr)
        free(obj->arr);
    obj->arr = NULL;
    free(obj);
    obj = NULL;
}